<?php

Yii::import('user.models.User');

/**
 * @author HungNQ
 */
class AttendanceController extends RController
{

    public function init()
    {
        parent::init();
        $baseUrl = Yii::app()->baseUrl;
        $cs = Yii::app()->getClientScript();
        $cs->registerScriptFile($baseUrl . '/js/jquery-ui-1.10.2.custom.min.js');
        $cs->registerCssFile($baseUrl . '/css/ui-lightness/jquery-ui-1.10.2.custom.min.css');
    }

    public function actionIndex()
    {
        $date = strtotime(date('m/d/Y'));
        $selected_date = date('m/d/Y');
        if (isset($_POST['date'])) {
            $date = strtotime($_POST['date']);
            $selected_date = $_POST['date'];
        }

        $data = Attendance::model()->getAttendanceByDate($date);

        $workshifts = Workshift::model()->findAll();

        if (isset($_POST['Attendance']) && isset($_POST['date']) && isset($_POST['save'])) {
            $attendances = $_POST['Attendance'];

            $date = strtotime($_POST['date']);

            foreach ($attendances as $user_id => $value) {
                $workshift = array();
                $workshift['1'] = intval($value['workshift1']);
                $workshift['2'] = intval($value['workshift2']);
                $workshift['3'] = intval($value['workshift3']);
                $this->_processAttendance($user_id, $workshift, $date);
            }
            //repopulate data
            $data = Attendance::model()->getAttendanceByDate($date);
        }


        $this->render('index', array(
            'rows' => $data,
            'date' => $selected_date,
            'workshifts' => $workshifts
        ));
    }

    private function _processAttendance($user_id, $workshift, $date)
    {
        foreach ($workshift as $workshift_id => $value) {
            $this->_insertAttendance($user_id, $workshift_id, $value, $date);
        }
    }

    private function _insertAttendance($user_id, $workshift_id, $value, $date)
    {
        $model = Attendance::model()->findByAttributes(array(
            'user_id' => $user_id,
            'date' => $date,
            'workshift_id' => $workshift_id
                ));

        if ($model == null) {
            $model = new Attendance;
        }

        $model->user_id = $user_id;
        $model->date = $date;
        switch ($value) {
            case 0://unknown
                $model->is_present = null;
                $model->workshift_id = $workshift_id;
                $model->is_paid_leave = null;
                break;
            case 1://present
                $model->is_present = 1;
                $model->workshift_id = $workshift_id;
                $model->is_paid_leave = null;
                break;
            case 2://paid leave
                $model->is_present = 0;
                $model->workshift_id = $workshift_id;
                $model->is_paid_leave = 1;
                break;
            case 3://unpaid leave
                $model->is_present = 0;
                $model->workshift_id = $workshift_id;
                $model->is_paid_leave = 0;
                break;
        }

        $model->description = "done";

        if ($model->validate()) {
            $model->save();
        }
    }

    public function actionMonth()
    {
        $date = strtotime(date('m/d/Y'));

        $selected_date = date('m/d/Y');

        if (isset($_POST['date'])) {
            $date = strtotime($_POST['date']);
            $selected_date = $_POST['date'];
        }

        $data = Attendance::model()->getAttendanceDetail($date);
        $users = $users = User::model()->with('profile')->findAll();

        $this->render('month', array(
            'rows' => $data,
            'users' => $users,
            'date' => $selected_date,
        ));
    }

    public function actionDetail()
    {
        $date = Yii::app()->getRequest()->getQuery('date');
        $user_id = Yii::app()->getRequest()->getQuery('user_id');
        $shift_pay = 50000;

        $number_of_shifts = Attendance::model()->getNumberOfWorkShifts($user_id, $date);
        $user = User::model()->findByPk($user_id);
        $total = $number_of_shifts * $shift_pay;

        $this->render('detail', array(
            'user' => $user,
            'date' => $date,
            'number_of_shifts' => $number_of_shifts,
            'total' => $total
        ));
    }

    public function actionReport()
    {
        $date = strtotime(date('m/d/Y'));

        $selected_date = date('m/d/Y');

        if (isset($_POST['date'])) {
            $date = strtotime($_POST['date']);
            $selected_date = $_POST['date'];
        }

        $this->render('report', array(
            'results' => Attendance::model()->getAttendanceReport($date),
            'workshifts' => Attendance::model()->getWorkshifts(),
            'date' => $selected_date,
        ));
    }

    public function actionReportDetail()
    {
        $month = Yii::app()->getRequest()->getQuery('month');
        $year = Yii::app()->getRequest()->getQuery('year');
        $user_id = Yii::app()->getRequest()->getQuery('user_id');

        $result = Attendance::model()->getReportDetailOfUser($month, $year, $user_id);

        $user = User::model()->findByPk($user_id);
        $this->render('report_detail', array(
            'result' => $result,
            'user' => $user,
            'workshifts' => Attendance::model()->getWorkshifts(),
            'month' => $month,
            'year' => $year
        ));
    }

    public function filters()
    {
        return array(
            'rights'
        );
    }

}
